package com.ontheroad.mysql.activity.service;

import cn.modoumama.service.base.impl.BaseServiceImpl;
import com.ontheroad.activity.model.ActivityModel;
import com.ontheroad.activity.model.ActivityTypeModel;
import com.ontheroad.activity.service.ActivitService;
import com.ontheroad.api.response.ActivtiyListResponse;
import com.ontheroad.api.response.JoinApplyResponse;
import com.ontheroad.core.util.DateUtils;
import com.ontheroad.mysql.activity.mapper.ActivityMapper;
import com.ontheroad.mysql.activity.mapper.ActivityTypeActMapper;
import com.ontheroad.mysql.activity.mapper.ActivityTypeMapper;
import com.ontheroad.user.model.UserModel;
import com.ontheroad.user.service.UserService;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by kedong on 2017/9/23 0023.
 */
@Service
@Transactional
public class ActivitServiceImpl extends BaseServiceImpl<ActivityModel, Integer> implements ActivitService {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private ActivityMapper mapper;

    @Autowired
    private ActivityTypeActMapper activityTypeActMapper;

    @Autowired
    private ActivityTypeMapper typeMapper;

    @Autowired
    private UserService userService;


    @Autowired
    public void setMapper(ActivityMapper mapper) {
        setGenericMapper(mapper);
    }

    @Override
    public List<ActivtiyListResponse> convertActivityList(List<ActivityModel> list) {
        List<ActivtiyListResponse> listResponses = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (ActivityModel activityModel : list) {
                ActivtiyListResponse response = new ActivtiyListResponse();
                // 活动ID
                response.setId(activityModel.getId());
                // 活动标题
                response.setTitle(activityModel.getTitle());
                response.setTitleImage(activityModel.getTitleImage());
                // 活动开始时间（标准时间戳）
                response.setStartTime(DateUtils.dateToTimestamp(activityModel.getStartTime()));
                Integer duration = DateUtils.getDiffDays(activityModel.getStartTime(), activityModel.getEndTime());
                // 活动持续时间（天）
                response.setDuration(duration > 0 ? duration : 0);
                // 活动状态
                response.setState(getActivityState(activityModel));
                // 活动目的地
                response.setAddress(activityModel.getAddress());
                // 查询活动类型
                List<ActivityTypeModel> types = activityTypeActMapper.findModelsByActivityId(activityModel.getId());
                response.setTypes(types);
                UserModel user = userService.findById(activityModel.getCreateUserId());
                // 是否为官方活动
                response.setOfficial(user.getOfficial());
                listResponses.add(response);
            }
        }
        return listResponses;
    }

    @Override
    public List<Integer> getActivityIdsByType(Integer type) {
        return activityTypeActMapper.getActivityIdsByType(type);
    }

    @Override
    public List<ActivityTypeModel> getTypeListByCondition(Map<String, Object> condition) {
        if (condition == null) {
            condition = new HashMap<>();
        }

        return typeMapper.findModelsByCondition(condition);
    }

    @Override
    public List<Integer> getActivityIdsByFreindJoin(Integer userId) {
        return mapper.getActivityIdsByFreindJoin(userId);
    }

    @Override
    public void saveActTypes(List<Integer> actTypes, Integer activityId) {
        activityTypeActMapper.saveActTypes(actTypes, activityId);
    }

    @Override
    public Integer getActivityState(ActivityModel activityModel) {
        int state = 0;
        Date now = new Date();
        //活动开始结束时间
        Date endTime = activityModel.getEndTime();
        Date startTime = activityModel.getStartTime();
        //活动报名开始结束时间
        Date createTime = activityModel.getCreateTime();
        if (now.after(createTime) && now.before(startTime)) {
            // 报名中
            state = 1;
        } else if (now.after(startTime) && now.before(endTime)) {
            // 活动正在进行中
            state = 2;
        } else if (now.after(endTime)) {
            // 活动结束
            state = 3;
        }

        return state;
    }

    @Override
    public List<JoinApplyResponse> getJoinApplyList(Integer userId, Integer pageNum, Integer pageSize) {
        return mapper.getJoinApplyList(userId, new RowBounds(pageSize * (pageNum - 1), pageSize));
    }

}
